<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="90" formType="add" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <FilterTable
      ref="table"
      height="auto"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      columnsRef="finance_repairFinance_receivables_receiptsDetail_writeOff"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
    </FilterTable>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog(true)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { getUserId, getUser } from '@/assets/js/auth';
import { getEmployee, receive, getReceiveDetail } from '@/api/finance/repair/receivables';
import { getWriteOffList, saveWriteOff, getAllUser } from '@/api/repair/repairFinance/index.js';

export default {
  name: 'WriteOff',
  mixins: [dictionary],
  props: ['type', 'selectRow'],
  data() {
    this.saveData = null;
    return {
      index: null,
      formList: this.createFormList(),
      columns: this.createTableColumns(),
      fetchParams: { xhrAbort: false, balaPayObjId: this.selectRow[0].BALA_PAYOBJ_ID },
      fetchApi: getWriteOffList
    };
  },

  mounted() {
    this.formList.find(x => x.fieldName === 'PAYMENT_OBJECT_CODE').initialValue = this.selectRow[0].PAYMENT_OBJECT_CODE;
    this.formList.find(x => x.fieldName === 'PAYMENT_OBJECT_NAME').initialValue = this.selectRow[0].PAYMENT_OBJECT_NAME;
    this.formList.find(x => x.fieldName === 'HANDLER').initialValue = getUser();
    this.getAllUser();
  },
  methods: {
    // 获取所有的人员
    async getAllUser() {
      let res = await getAllUser();
      if (res.resultCode == 200) {
        let allUser = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
        this.columns.find(item => item.dataIndex === 'HANDLER').dictItems = allUser;
        this.columns.find(item => item.dataIndex === 'INVOICE_MAKER').dictItems = allUser;
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '收费对象代码',
          fieldName: 'PAYMENT_OBJECT_CODE',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '收费对象名称',
          fieldName: 'PAYMENT_OBJECT_NAME',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经办人',
          fieldName: 'HANDLER',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" disabled={props.row.WRITEOFF_TAG == 10041001} type="text" onClick={() => this.writeOff(props.row)}>
                  销账
                </el-button>
              </div>
            );
          }
        },
        {
          title: '收款单号',
          dataIndex: 'RECEIVE_NO',
          sorter: true
        },
        {
          title: '收款日期',
          dataIndex: 'RECEIVE_DATE',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '收款金额',
          dataIndex: 'RECEIVE_AMOUNT',
          precision: 2,
          sorter: true
        },
        {
          title: '销帐标志',
          dataIndex: 'WRITEOFF_TAG',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.WRITEOFF_TAG, '1004')}</span>;
          }
        },
        {
          title: '经手人',
          dataIndex: 'HANDLER',
          sorter: true,
          dictItems: []
        },
        {
          title: '付款方式',
          dataIndex: 'PAY_TYPE_CODE',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.PAY_TYPE_CODE, '8044')}</span>;
          }
        },
        // 是否开票、发票类型、发票号码、开票人
        {
          title: '本次使用余款',
          dataIndex: 'USE_PRE_PAY',
          sorter: true
        },
        {
          title: '是否开票',
          dataIndex: 'INVOICE_TAG',
          sorter: true,
          render: props => {
            return <span>{this.createDictText(props.row.INVOICE_TAG, '1004')}</span>;
          }
        },
        {
          title: '发票类型',
          dataIndex: 'INVOICE_TYPE_CODES',
          sorter: true,
          render: props => {
            let dict = this.createDictList('1111');
            // 翻译发票类型
            let str = props.row.INVOICE_TYPE_CODES;
            if (props.row.INVOICE_TYPE_CODES.length) {
              let types = props.row.INVOICE_TYPE_CODES.split(',');
              let result = [];
              types.forEach(x => {
                let item = dict.find(item => item.value == x);
                if (item) {
                  result.push(item.text);
                }
              });
              str = result.join(',');
            }
            return <div>{str}</div>;
          }
        },
        {
          title: '发票号码',
          dataIndex: 'INVOICE_NOS',
          sorter: true
        },
        {
          title: '开票人',
          dataIndex: 'INVOICE_MAKER',
          sorter: true,
          dictItems: []
        },
        {
          title: '支票号码',
          dataIndex: 'CHECK_NO',
          sorter: true
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true
        }
      ];
    },

    formChangeHandle(val) {
      this.saveData = val;
    },
    async getDealerUser() {
      const res = await getDealerUser();
      console.log(res, '所有用户');
      let allUser = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'HANDLER').editItems = allUser;
    },
    async writeOff(data) {
      this.$confirm('是否确认销账?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await saveWriteOff(data.RECEIVE_NO);
          console.log('res', res);
          if (res.resultCode == 200) {
            this.$notify.success({ title: '提示信息', message: '销账成功!' });
            this.fetchParams = { ...this.fetchParams };
          }
        })
        .catch(() => {});
    },

    closeDialog(val) {
      this.$emit('close', val);
    }
  }
};
</script>

<style lang="less" scoped></style>
